/**
 * 页面加载
 */
$(function() {
	//panel初始化
	$('#dAuthorityPanel').panel({
		border:false
	});
	
	//菜单按钮初始化
	$('#saveAuthorityBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeAuthorityBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	
	
	//初始化角色列表
	$('#autRoleDataGrid').datagrid({
		url:'roleAjax!loadRoleData',
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'desc',
		remoteSort: false,
		pagination:true,
		idField:'id',			
		rownumbers:true,
		columns:[[
		           {field:'name',title:'名称',width:200},
		           {field:'code',title:'代码',width:100},
		           {field:'des',title:'描述',width:240}
				]],
		onClickRow:function(rowIndex, rowData){
					$('#roleId').val(rowData.id);
					$('#autMenuDataTree').tree({url:'permissionAjax!loadTreeData?roleId='+rowData.id});
					$('#autFunctionDataGrid').datagrid('uncheckAll');
				},
		onLoadSuccess:function(data){
					$('#autRoleDataGrid').datagrid('selectRow', 0);
		}
	});
		
	
	//分页
	var pager = $('#autRoleDataGrid').datagrid('getPager');
	$(pager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			$('#autRoleDataGrid').datagrid('options').url = 'roleAjax!loadRoleData?pageNo=' + pageNumber + '&pageSize='+ pageSize;
			$('#autRoleDataGrid').datagrid('reload', pageNumber);
		}
	});
	
	
	//权限菜单树初始化
	$('#autMenuDataTree').tree({
		checkbox:true,
		url:'permissionAjax!loadTreeData',
		loadMsg:LOADING_MESSAGE,
		border:false,
		onDblClick: function(node){
			$(this).tree('toggle',node.id);
		}
	});
	
});

//保存权限
function openAuthorityWindow(type){
	
	var selectedRole = $('#autRoleDataGrid').datagrid('getSelected');
	if(selectedRole==null){
		$.messager.alert('提示','请先选择一条记录!');
		return;
	}
	if(selectedRole.systemState){
		$.messager.alert('提示','系统默认角色不允许编辑!');
		return;
	}

	var roleId = selectedRole.id;

	//删除历史权限
	$.post('permissionAjax!remove', {roleId:roleId},		    		
        	function(data){	
				//删除成功 保存
				savePerm(roleId);
	}, 'json');


	
}

//保存
function savePerm(roleId){
	var nodes = $('#autMenuDataTree').tree('getChecked');
	var subMenuNodeIds = '';
	var mainMenuNodeIds = '';
	var funNodeIds = '';
	
	for(var i=0; i<nodes.length; i++){
		if(nodes[i].id.substr(0, 1)=='s'){
			if (subMenuNodeIds != '') subMenuNodeIds += ',';
				var nodeId = nodes[i].id;
				subMenuNodeIds += nodeId.substr(1,nodeId.length);
		}else if(nodes[i].id.substr(0, 1)=='m'){
			if (mainMenuNodeIds != '') mainMenuNodeIds += ',';
				var nodeId = nodes[i].id;
				mainMenuNodeIds += nodeId.substr(1,nodeId.length);
		}
	}
	
	//父实心节点
	var nodes1 = [];
	 $("#autMenuDataTree").find('.tree-checkbox2').each(function(){
	        var node = $(this).parent();
	        nodes1.push($.extend({}, $.data(node[0], 'tree-node'), {
	                target: node[0],
	                checked: node.find('.tree-checkbox').hasClass('tree-checkbox2')
	        }));
	});

	for(var i=0; i<nodes1.length; i++){
		if(nodes1[i].id.substr(0, 1)=='s'){
			if (subMenuNodeIds != '') subMenuNodeIds += ',';
				var nodeId = nodes1[i].id;
				subMenuNodeIds += nodeId.substr(1,nodeId.length);
		}else if(nodes1[i].id.substr(0, 1)=='m'){
			if (mainMenuNodeIds != '') mainMenuNodeIds += ',';
				var nodeId = nodes1[i].id;
				mainMenuNodeIds += nodeId.substr(1,nodeId.length);
		}
	}
	
	var subNodes = subMenuNodeIds.split(",");
	//子菜单数组遍历
	for(var i = 0; i < subNodes.length; i++){
		//根据子菜单ID 获取 子菜单对象
		var subMenuNode = $('#autMenuDataTree').tree('find', 's'+subNodes[i]);
		//根据子菜单获取所有功能
		var children = $('#autMenuDataTree').tree('getChildren', subMenuNode.target);
		//初始化功能字符串
		funNodeIds = '';
		
		//遍历 子菜单下所有功能
		for(var j = 0; j < children.length; j++){
			//判断功能是否被选择
			if(children[j].checked){
				if(funNodeIds != '') funNodeIds += ',';
				var funNodeId = children[j].id;
				funNodeIds += funNodeId.substr(1, funNodeId.length);
			}
		}

		$.post('permissionAjax!save', {roleId:roleId, menuId:subNodes[i], funIds:funNodeIds},		    		
	        	function(data){
					flag = data.flag;
					//alert(flag);
					if(flag == "false"){
						MESSAGEBOX('权限保存失败!');
						return;
					}else{
						MESSAGEBOX('权限保存成功!');
					}
				}
		, 'json');
	}
}